/*
 * @Description:
 * @Author: Cxy
 * @Date: 2025-02-22 15:31:09
 * @LastEditors: Cxy
 * @LastEditTime: 2025-10-15 22:36:47
 * @FilePath: \xy-admin\docs\.vitepress\theme\index.ts
 */
import { App, h } from 'vue'
import DefaultTheme from 'vitepress/theme'
import leftLogo from './leftLogo.vue'
import XyElement from '@xy-admin/elecomponents'
import { ElConfigProvider } from 'element-plus'
import Demo from '../plugin/demo/index.vue'
import ToolTip from '../plugin/tooltip/index.vue'
import './style.scss'
import 'prismjs/themes/prism.min.css'
import 'element-plus/theme-chalk/src/dark/css-vars.scss'
import '@xy-admin/elecomponents/dist/style.css'
import { XyIconRender } from '@/components/XyIcon'
import zhCn from 'element-plus/es/locale/lang/zh-cn'

export default {
  extends: DefaultTheme,
  Layout() {
    return h(
      ElConfigProvider,
      { locale: zhCn },
      {
        default: () =>
          h(DefaultTheme.Layout, null, {
            'home-hero-image': () => h(leftLogo)
          })
      }
    )
  },
  enhanceApp({ app }: { app: App }) {
    // 注册自定义全局组件
    app.use(XyElement, {
      form: {
        addForm: {
          column: 1
        },
        datePicker: { defaultTime: [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)] },
        upload: {
          uploadMethod: options => {
            console.log(options)
          }
        }
      },
      table: {
        pageConf: {
          currentPageAlias: 'pageNum',
          prevIcon: XyIconRender('streamline:button-previous-solid'),
          nextIcon: XyIconRender('streamline:button-next-solid')
        },
        loadingConf: {
          target: 'tablePag'
        },
        emptySlot: () =>
          h(
            'div',
            { style: { display: 'flex', margin: 'auto', alignItems: 'center', height: '120px', width: '170px' } },
            [h(leftLogo)]
          )
      }
    })
    app.component('Demo', Demo)
    app.component('ToolTip', ToolTip)
    app.component('ElConfigProvider', ElConfigProvider)

    const templates = import.meta.glob(`../../elecomponents/**/*.vue`, {
      import: 'default',
      eager: true
    })
    app.provide(
      'templates',
      Object.keys(templates).reduce(
        (prev, c) => ({
          ...prev,
          [c.replace(/.*elecomponents\/([^\/]+\/[^\.]+).*/, '$1')]: templates[c]
        }),
        {}
      )
    )
  }
}
